package database

import (
	"gin-xorm/log"
	"os"
	"strconv"

	"github.com/go-ini/ini"
	"github.com/piaohao/godis"
)

var r *godis.Redis

type RLog struct {
	log.Log
}

var rLog RLog

func init() {
	rLog.Tag = "Redis"

	cfg, err := ini.Load("config/config.ini")
	if err != nil {
		rLog.Error(err)
	}

	host := cfg.Section("redis").Key("host").Value()
	port, _ := strconv.Atoi(cfg.Section("redis").Key("port").Value())
	password := cfg.Section("redis").Key("password").Value()
	db, _ := strconv.Atoi(cfg.Section("redis").Key("db").Value())

	option := &godis.Option{
		Host:     host,
		Port:     port,
		Password: password,
		Db:       db,
	}

	pool := godis.NewPool(&godis.PoolConfig{}, option)
	r, _ = pool.GetResource()
	defer r.Close()

	if err != nil {
		rLog.Error("Start redis client err", err)
		os.Exit(-1)
	}
}
